# /usr/bin/python
# -*- coding: utf-8 -*-

import os,sys
BASE_DIR = os.path.dirname(os.path.abspath(os.curdir))
sys.path.append(BASE_DIR)
reload(sys)  
sys.setdefaultencoding('utf8')   
from models.Zabbix import ZBX
from models.MySQL import MySQL
from util.log import get_stream_logger

logger = get_stream_logger("add_hostgroup")

#host="10.205.140.41",port=3306,user="mysql",passwd="mysql888",db="cmdb"

with MySQL() as cur:
    cmd = "SELECT HGNAME,HGId FROM server_hostgroup"
    cmdb_group = cur.search(cmd)

with MySQL() as cur:
    for group in cmdb_group:
        check_cmd = "SELECT * from grp WHERE grp_name='%s'"%group[0]
        check_res = cur.cur.execute(check_cmd)
        if check_res == 0:
            insert_cmd = "INSERT INTO grp VALUES (null,'%s','root',null,'0')"%group[0]
            try:
                res = cur.update(insert_cmd)
                if res != 1:
                    logger.error("Insert into grp: %s "%group[0])
            except Exception:
                print insert_cmd

